package com.draven.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.draven.common.BusinessException;
import com.draven.entity.NoteCategory;
import com.draven.mapper.NoteCategoryMapper;
import com.draven.service.NoteCategoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 笔记分类服务实现类
 */
@Service
public class NoteCategoryServiceImpl extends ServiceImpl<NoteCategoryMapper, NoteCategory> implements NoteCategoryService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long add(Long userId, String name, Integer sort, String icon) {
        // 创建分类
        NoteCategory category = new NoteCategory();
        category.setUserId(userId);
        category.setName(name);
        category.setSort(sort);
        category.setIcon(icon);

        save(category);
        return category.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long add(NoteCategory category) {
        if (StringUtils.isBlank(category.getName())) {
            throw new BusinessException(400, "分类名称不能为空");
        }
        save(category);
        return category.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Long id, Long userId, String name, Integer sort, String icon) {
        // 查询分类
        NoteCategory category = getDetail(id, userId);

        // 更新信息
        category.setName(name);
        category.setSort(sort);
        category.setIcon(icon);

        updateById(category);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(NoteCategory category) {
        if (category.getId() == null) {
            throw new BusinessException(400, "ID不能为空");
        }
        if (StringUtils.isBlank(category.getName())) {
            throw new BusinessException(400, "分类名称不能为空");
        }

        // 查询分类并验证所有权
        NoteCategory existingCategory = getDetail(category.getId(), category.getUserId());

        // 更新信息
        category.setCreateTime(existingCategory.getCreateTime());
        updateById(category);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id, Long userId) {
        // 查询分类
        NoteCategory category = getDetail(id, userId);

        // 删除分类
        removeById(id);
    }

    @Override
    public NoteCategory getDetail(Long id, Long userId) {
        // 查询分类
        NoteCategory category = getById(id);
        if (category == null) {
            throw new BusinessException(400, "分类不存在");
        }

        // 校验所有权
        if (!category.getUserId().equals(userId)) {
            throw new BusinessException(403, "无权限操作");
        }

        return category;
    }

    @Override
    public List<NoteCategory> listByUserId(Long userId) {
        // 构建查询条件
        LambdaQueryWrapper<NoteCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NoteCategory::getUserId, userId);
        wrapper.orderByAsc(NoteCategory::getSort);
        wrapper.orderByDesc(NoteCategory::getCreateTime);

        return list(wrapper);
    }

    @Override
    public Page<NoteCategory> page(Long userId, String name, Integer pageNum, Integer pageSize) {
        // 构建查询条件
        LambdaQueryWrapper<NoteCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NoteCategory::getUserId, userId);
        wrapper.like(StringUtils.isNotBlank(name), NoteCategory::getName, name);
        wrapper.orderByAsc(NoteCategory::getSort);
        wrapper.orderByDesc(NoteCategory::getCreateTime);

        // 分页查询
        return page(new Page<>(pageNum, pageSize), wrapper);
    }
} 